package br.com.napoleao.controlfin.form;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import br.com.napoleao.controlfin.core.business.bo.CategoriaBO;
import br.com.napoleao.controlfin.core.entity.CategoriaEntity;
import br.com.napoleao.controlfin.core.exception.BusinessException;
import br.com.napoleao.controlfin.core.persistence.dao.CategoriaDAO;
import br.com.napoleao.controlfin.util.GUIUtil;

import com.swtdesigner.SWTResourceManager;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class SalvarCategoriaDialog extends Dialog {

	protected Object result;
	protected Shell shell;
	private Text txtNome;
	private CategoriaEntity categoria;
	
	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public SalvarCategoriaDialog(Shell parent, int style) {
		super(parent, style);
		setText("Salvar Categoria");
	}

	/**
	 * Open the dialog.
	 * @return the result
	 */
	public Object open() {
		createContents();
		
		GUIUtil.centralizeShell(shell, shell.getParent().getShell());
		
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), getStyle());
		shell.setSize(322, 107);
		shell.setText(getText());
		shell.setLayout(null);
		
		txtNome = new Text(shell, SWT.BORDER);
		txtNome.setBounds(68, 10, 238, 21);
		
		Label lblNome = new Label(shell, SWT.NONE);
		lblNome.setAlignment(SWT.RIGHT);
		lblNome.setBounds(7, 13, 55, 15);
		lblNome.setText("Nome:");
		
		Button btnSalvar = new Button(shell, SWT.NONE);
		btnSalvar.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				botaoSalvarCat();
			}
		});
		btnSalvar.setImage(SWTResourceManager.getImage(SalvarCategoriaDialog.class, "/br/com/napoleao/controlfin/form/resources/salvar.png"));
		btnSalvar.setBounds(231, 44, 75, 25);
		btnSalvar.setText("Salvar");

		popularObjetoFormulario();
	}

	public void recuperaObjetoFormulario(){
		if(categoria == null)
			categoria = new CategoriaEntity();
		
		if(txtNome.getText() != null && !txtNome.getText().equals("")) categoria.setNome(txtNome.getText());
	}
	
	public void popularObjetoFormulario(){
		if(categoria != null && categoria.getNome() != null) txtNome.setText(categoria.getNome());
	}
	
	public void botaoSalvarCat(){
		
		recuperaObjetoFormulario();
		
		try {
			new CategoriaBO().validarObjeto(categoria);
		} catch (BusinessException e) {
			MessageDialog.openError(shell, "Erro ao cadastrar", e.getMessagesString());	
			return;
		}
		
		if(categoria != null && categoria.getId() == null)
			new CategoriaDAO().insert(categoria);
		else{
			new CategoriaDAO().update(categoria);
		}
		
		shell.dispose();
	}
	
	public CategoriaEntity getCategoria() {
		return categoria;
	}

	public void setCategoria(CategoriaEntity categoria) {
		this.categoria = categoria;
	}
}
